// -*- coding: utf-8 -*- 
/**
 * Project: LanQiao
 * Creator: yanking
 * Create time: 2022-01-21 16:43
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.GroupByCollect;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;

public class Grouping {
    @Test
    public void shout() {
        // 测试简化构造器枚举类toString方法
        System.out.printf(String.valueOf(transactions.currency.YUAN)); // YUAN
        System.out.printf(String.valueOf(transactions.currency.YUAN)); // currency{type='人民币：元'}

    }


    @Test
    public void shout01() {
        // 按照货币的类型对订单进行分类
        transactions.currency Y = transactions.currency.YUAN;
        transactions.currency D = transactions.currency.DOLLARS;
        transactions.currency G = transactions.currency.GOLD;
        List<transactions> tts = Arrays.asList(
                new transactions(Y, 568.568),
                new transactions(Y, 999.568),
                new transactions(Y, 888.568),
                new transactions(Y, 666.568),
                new transactions(D, 123.123),
                new transactions(G, 888.888)
        );
       /* // grouping
        HashMap<com.GroupByCollect.transactions.currency, List<com.GroupByCollect.transactions>> currencyListHashMap =
                new HashMap<>();
        for (transactions transaction : transactions) {
            transactions.currency currency = transaction.getCurrency();
            List<com.GroupByCollect.transactions> transactionConcurrency = currencyListHashMap.computeIfAbsent(currency, k -> new ArrayList<>());
            transactionConcurrency.add(transaction);
        }
        for (com.GroupByCollect.transactions.currency currency : currencyListHashMap.keySet()) {
            System.out.println("The currency :" + currency);
            currencyListHashMap.get(currency).forEach(System.out::println);
        }*/
        Map<transactions.currency, List<transactions>> res =
                tts.stream().collect(groupingBy(transactions::getCurrency));
        for (transactions.currency currency : res.keySet()) {
            System.out.println("The currency :" + currency);
            res.get(currency).forEach(System.out::println);
        }

    }

    @Test
    public void shout02() {
        List<Integer> res = Arrays.asList(1, 2, 3, 4, 5, 6);
        String collect = res.stream().map(Object::toString).collect(Collectors.joining("->"));
        System.out.println(collect);
    }

    @Test
    public void shout03() {
        StringJoiner sj = new StringJoiner("->", ":", "<-");
        sj.add("Geo").add("jack").add("jim");
        System.out.println(sj);
    }

    @Test
    public void shout04() {
        StringJoiner sj = new StringJoiner(":", "[", "]");
        sj.add("George").add("Sally").add("Fred");
        String desiredString = sj.toString();
        System.out.println(desiredString);
    }


    @Test
    public void shout05() {
        StringJoiner sj = new StringJoiner("->");
        Stream.of(1, 2, 3, 4, 5).map(Object::toString).forEach(sj::add);
        System.out.println(sj);
    }

    @Test
    public void shout06() {
        // try to define a inner iterate in a inner class in a method
        class persons {
            private String name;
            private int age;

            public persons(String name, int age) {
                this.name = name;
                this.age = age;
            }

            @Override
            public String toString() {
                return "persons{" +
                        "name='" + name + '\'' +
                        ", age=" + age +
                        '}';
            }
        }
        System.out.println(new persons("xiaoMing", 18).toString());


    }

    @Test
    public void shout07() {
        test test = new test();
        Grouping grouping = new Grouping();
        System.out.println(test instanceof Grouping);
    }

    @Test
    public void shout08() {

    }

}

class test extends Grouping {
}
